﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lapas.Common.Forms;
//using Lapas.Common;
using WorkDAL;
using System.Globalization;
using System.Threading;
using Common;
using Lapas.Common;

namespace WorksList
{
    public partial class WorkEditForm : EditForm
    {
        string userEnter = CommonSettings.UserName;

        public WorkObject workObject = new WorkObject();

        public WorkEditForm()
        {
            InitializeComponent();

            CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            culture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
            culture.DateTimeFormat.LongDatePattern = "dd MMMM, yyyy";
            Thread.CurrentThread.CurrentCulture = culture;

            base.OkButton = btnOK;
            base.CloseButton = btnClose;

            dtOpenDate = UIFormat.Format_NullableDateTimePicker(dtOpenDate, DateTime.Today);
            dtExpectedDate = UIFormat.Format_NullableDateTimePicker(dtExpectedDate, DateTime.Today);
            dtEndDate = UIFormat.Format_NullableDateTimePicker(dtEndDate);
        }

        private void WorkEditForm_Load(object sender, EventArgs e)
        {
            this.cbStatus.DataSource = WorksDataAccess.Instance().GetTypes(string.Empty);
            this.cbStatus.DisplayMember = "Descriptions";
            this.cbStatus.ValueMember = "Types";
        }

        private Boolean CheckData()
        {
            if (txtTitle.Text.Trim() == string.Empty)
            {
                er.SetError(txtTitle, "Không được để trống công việc");
                return false;
            }

            if (dtOpenDate.Value == null)
            {
                er.SetError(dtOpenDate, "Chưa nhập ngày tạo");
                return false;
            }

            if (dtExpectedDate.Value == null)
            {
                er.SetError(dtExpectedDate, "Chưa nhập ngày dự kiến hoàn thành");
                return false;
            }
            return true;
        }

        protected override void BindData()
        {
            if (base.FormAction != EditFormAction.Insert)
            {
                txtDescriptions.Text = workObject.Descriptions;
                txtTitle.Text = workObject.Title;
                if (workObject.EndDate != null)
                    dtEndDate.Value = (DateTime)workObject.EndDate;
                dtExpectedDate.Value = workObject.ExpectedDate;
                dtOpenDate.Value = workObject.OpenDate;
                cbStatus.SelectedValue = workObject.Status;
            }
        }

        protected override bool DoCreate()
        {
            try
            {
                if (!CheckData()) return false;

                Work work = new Work();
                work.Descriptions = txtDescriptions.Text.Trim() != null? txtDescriptions.Text.Trim() : null;
                work.OpenDate = (DateTime)dtOpenDate.Value;
                work.ExpectedDate = (DateTime)dtExpectedDate.Value;
                work.Status = cbStatus.SelectedValue.ToString();
                work.Title = txtTitle.Text.Trim();
                work.User = userEnter;
                work.ID = Guid.NewGuid();

                if (dtEndDate.Value != null) work.EndDate = (DateTime)dtEndDate.Value;

                WorksDataAccess.Instance().CreateWork(work);
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
            }

            return true;
        }

        protected override bool DoUpdate()
        {
            try
            {
                if (!CheckData()) return false;

                Work work = new Work();
                work.Descriptions = txtDescriptions.Text.Trim() != null? txtDescriptions.Text.Trim() : null;
                work.OpenDate = (DateTime)dtOpenDate.Value;
                work.ExpectedDate = (DateTime)dtExpectedDate.Value;
                work.Status = cbStatus.SelectedValue.ToString();
                work.Title = txtTitle.Text.Trim();
                work.User = userEnter;
                work.ID = workObject.ID;

                if (dtEndDate.Value != null) work.EndDate = (DateTime)dtEndDate.Value;

                WorksDataAccess.Instance().UpdateWork(work);
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
            }

            return true;
        }

    }
}
